package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"gin/Databases"
	"gin/Common"
)

type EntityDataSuggestCgi struct {
	Common.CgiBase
}


func (cgi EntityDataSuggestCgi)addSuggestEntity() {
	fmt.Println("addEntity:", cgi.Input["name"])

	sql := "insert into db_entity_suggest (Fname,Fuser,Furl,Fentity_set,Fstatus,Fcreate_time,Fmodify_time) values (?,?,?,?,0,now(),now());"
	_,err := Databases.Db.Exec(sql, cgi.Input["name"], cgi.Input["user"], cgi.Input["url"], cgi.Input["entity_set"])
	if err != nil {
		panic("重复添加")
	}
}


func (cgi *EntityDataSuggestCgi)processParam(c *gin.Context) {
	fmt.Println("processParam")
	cgi.Input["name"] = c.PostForm("name")
	cgi.Input["user"] = c.PostForm("user")
	cgi.Input["url"] = c.DefaultPostForm("url", "")
	cgi.Input["entity_set"] = c.DefaultPostForm("entity_set", "")
}

// 实体集数据
func EntityDataSuggest(c *gin.Context) {
	defer func(){
		if err:=recover();err!=nil{
			fmt.Println("error:",err)
			c.JSON(http.StatusOK, gin.H{
		        "result": -1,
		        "msg":    err,
		        "data": "{}",
		    })
		}
	}()

	var cgi EntityDataSuggestCgi
	cgi.Init(c)
	cgi.processParam(c)

    cgi.addSuggestEntity()

    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": "",
    })

}

